package org.ssssssss.magicapi;

import org.restrpc.client.RestRpc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.ssssssss.magicapi.core.config.MagicAPIProperties;
import org.ssssssss.magicapi.core.config.MagicPluginConfiguration;
import org.ssssssss.magicapi.core.model.Plugin;
import org.ssssssss.magicapi.core.service.MagicAPIService;

@Configuration
public class MagicRestrpcConfiguration implements MagicPluginConfiguration {

	private final MagicAPIProperties properties;

	@Autowired
	private MagicAPIService service;
	

	@Value("${magic-api.restrpc.init-start:false}")
	public boolean initStart;
	@Value("${magic-api.restrpc.libPath:}")
	public String libPath;
	@Value("${magic-api.restrpc.ip:127.0.0.1}")
	public String ip;
	@Value("${magic-api.restrpc.port:9000}")
	public int port;
	@Value("${magic-api.restrpc.messageApi}")
	public String messageApi;
	
	public MagicRestrpcConfiguration(MagicAPIProperties properties) {
		this.properties = properties;
	}

	@Override
	public Plugin plugin() {
		System.out.println("Restrpc插件:initStart="+initStart);
		if(service!=null) {
			RestRpc.setService(service);
			RestRpc.setApi(messageApi);
			if(initStart) {
				if(RestRpc.loadLib(libPath)) {
					System.out.println("RestRpc库加载成功");
				}
				else System.out.println("RestRpc库加载失败");
			}
		}
		return new Plugin("Restrpc");
	}
}
